<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

use h1cms\H1;
use h1cms\system\Auth;
use Lg\Http\Input;
use Lg\View\View;

// +----------------------------------------------------------------------
// | Admin Header
// +----------------------------------------------------------------------
function get_header() {
    return View::make('@app/system/views/header');
}

// +----------------------------------------------------------------------
// | Admin Footer
// +----------------------------------------------------------------------
function get_footer() {
    return View::make('@app/system/views/footer');
}

// +----------------------------------------------------------------------
// | User Login
// +----------------------------------------------------------------------
function lg_user_login($redirect = NULL) {
    redirect_to('/auth/login.php', $redirect ? ['redirect' => $redirect] : NULL);
}

// +----------------------------------------------------------------------
// | User Logout
// +----------------------------------------------------------------------
function lg_user_logout($redirect = NULL) {
    redirect_to('/auth/logout.php', $redirect ? ['redirect' => $redirect] : NULL);
}

// +----------------------------------------------------------------------
// | System Module
// +----------------------------------------------------------------------
function lg_admin_system_modules() {
    return ['users', 'node', 'system', 'auth', 'dashboard', 'appearance', 'plugins'];
}

function lg_admin_default_menus() {
    return ['users', 'node', 'system', 'auth', 'dashboard', 'appearance', 'plugins'];
}

function lg_system_init() {
    //增加默认Breadcrumb
    screen()->addBreadcrumb(__('控制面板'), url_for('/dashboard/'), 'fa fa-home');
    //初始化左侧菜单
    screen()->menus = lg_create_menu();
}

function lg_create_menu() {
//    if (h1cms\system\Auth::isAdministrator()) {
    //
	//    }
    //登录的用户ID
    $uid = Auth::user()->id;
    //检查缓存
    $user_menu = \Lg\Cache\Cache::disk()->getItem('__admin_menu_for_' . $uid);
    if ($user_menu->isHit()) {
        return $user_menu->get();
    }
    $all_modules = lg_get_all_menus();
//    foreach ($all_modules as $key => $row) {
//        $sort_order[$key] = $row['sort_order'];
//    }
//    array_multisort($sort_order, SORT_ASC, SORT_NUMERIC, $all_modules);
    $user_menu->set($all_modules);
    $user_menu->save();
    return $all_modules;
}

function lg_sidebar_resolve_user_link() {
    $module_name = module_name();
    $script_name = basename(H1::$app->req->getScriptName());
    $action = Input::get('action');
    $link = '/' . $module_name . '/' . $script_name;
}

function lg_get_all_menus() {
//    $modules = lg_system_get_all_modules();
    $modules = lg_get_all_modules();
    //include module menus
    $admin_sidebar_menus = [];
    foreach ($modules as $module_name) {
        $menu_file = APP_PATH . '/' . $module_name . '/config/menus.php';
        if (file_exists($menu_file)) {
            $section = include $menu_file;
            if (is_array($section)) {
                $admin_sidebar_menus[$module_name] = $section;
            }
        }
    }
    return $admin_sidebar_menus;
}

function lg_get_all_modules() {    
    $user_modules = db()->getCol('select module_name from {modules} where status=1 order by sort_order asc');
    if (!$user_modules) {
        $user_modules = [];
    }
    return $user_modules;
}

function lg_system_get_all_modules() {
    //系统模块 默认加载
    $system_modules = lg_admin_system_modules();
    //用户自定义模块
    $user_modules = db()->getCol('select module_name from {modules} where status=1 order by sort_order asc');
    if (!$user_modules) {
        $user_modules = [];
    }
    $system_modules = array_merge($system_modules, $user_modules);

    return $system_modules;
}

/**
 * 获取属性值
 * @param array $_search_option_name
 * @return array
 */
function system_get_options($_search_option_name = []) {
    $in_array = db()->quote($_search_option_name);
    $query = db()->getAll("select * from {options} where option_group IN ({$in_array})");
    $options = [];
    foreach ($query as $row) {
        $options[$row->option_group][$row->option_name] = $row->option_value;
    }
    return $options;
}

/**
 * 更新保存的Option
 * @param array $options 系统最新的属性
 * @param array $group_names 需要保存的组名
 */
function system_update_options($group_names = []) {
    $all_options = system_get_options($group_names);
    foreach ($group_names as $option_group_name) {
        //获取当前组，所有属性名和属性值
        $group_options = Input::post($option_group_name, []);
        if (is_scalar($group_options)) {
            continue;
        }
        foreach ($group_options as $key => $value) {
            if (\Lg\Support\Arr::has($all_options, "{$option_group_name}.{$key}")) {
                db()->update('options', ['option_value' => $value], ['option_group' => $option_group_name, 'option_name' => $key]);
            } else {
                db()->insert('options', ['option_value' => $value, 'option_group' => $option_group_name, 'option_name' => $key]);
            }
        }
    }
}

function get_system_temp_dir() {
    return STORAGE_DIR . 'tmp/';
}

//状态
function status_span($status) {
    switch ($status) {
        case 1:
            return '<span class="label label-success"><span class="fa fa-check"></span></span>';
        case 0:
            return '<span class="label label-danger"><span class="fa fa-close "></span></span>';
        default:
            return '<span class="label label-default">NONE</span>';
    }
}
